import url from "../../../config/url";
import axios from "axios";
axios.defaults.baseURL = url.apiUrl;
import listApi from "../../../api/enterprise";
// axios.defaults.headers.common['token'] = "666";
const enterpriseinfo = {
  namespaced: true,
  state: {
    list: [], //数据列表
    isSearch: false,
    params: {
      //关键字查询参数
      data: ""
    },
    modifyParams: {},
    pageSize: "10",
    pageNum: 1,
    status:0,//公司状态，默认为0待确认
    loading: false, //加载中...控制器
    finished: false, //所有数据加载完成\
    deleteid:null, //删除时用的企业id
    finishedText: "数据全部加载完成", //所有数据加载完成
    title: "企业信息", //页面标题栏，可以不写，标题栏会以路由中的title为默认
    dataStatus: "-1", //状态 0:数据为空;1:网络错误 -1:不显示(默认)
    updateCorpStatus: {
      //添加，修改企业状态
      text: "",
      code:'',
      data: {}
    }
  },
  mutations: {
    setstatus(state,data){
      //保存状态
      state.status = data
    },
    mutFinished(state, payload) {
      //控制fininsh状态
      state.finished = payload.finished;
    },
    mutLoading(state, payload) {
      //控制loading状态
      state.loading = payload.loading;
    },
    mutIsSearch(state, payload) {
      //是否是搜索
      state.isSearch = payload.isSearch;
    },
    mutDeleteid(state,payload){
      //添加删除id
      state.deleteid = payload
    },
    mutClearDeleteid(state){
      //清除删除id
      state.deleteid = null
    },
    mutList(state, payload) {
      //数据展示列表 payload:{data:数据，search:是否是搜索}
      state.list = state.list.concat(payload.data);
    },
    mutClearList(state) {
      //清空列表
      state.list = [];
      state.finished = false;
    },
    mutParams(state, payload) {
      //查询参数
      state.params.data = payload.data;
    },
    mutDataStatus(state, payload) {
      //列表数据状态
      state.dataStatus = payload.status;
    },
    mutUpdateCorpStatus(state, payload) {
      state.updateCorpStatus.data = payload;
    },
    mutDataStatus(state, payload) {
      state.dataStatus = payload.status;
    },
    mutReset(state) {
      state.loading = false;
      state.finished = false;
      state.dataStatus = "-1";
      state.isSearch = false;
    }
  },

  actions: {
    async actSearchList(ctx) {
      //关键字查询公司

      ctx.commit("mutLoading", {
        loading: true
      });
      // "?pageSize="+ctx.state.pageSize+"&pageNum="+ctx.state.pageNum
      await axios
        .get(
          listApi.findCorpList +
            "?pageSize=" +
            ctx.state.pageSize +
            "&pageNum=" +
            ctx.state.pageNum +
            "&name=" + ctx.state.params.data +
            "&status=" +
            ctx.state.status
        )
        .then(response => {
          let res = response.data;
          if (res.code === 200) {
            if (typeof res.data == "string") {
              ctx.state.updateCorpStatus.text = res.data;
              ctx.commit("mutLoading", {
                loading: false
              });
              ctx.commit("mutFinished", {
                finished: true
              });
              ctx.commit("mutDataStatus", {
                status: "0"
              });
              return null;
            }
            if (
              res.data.result == undefined ||
              (res.data.result.length == "0" && ctx.state.list.length == "0")
            ) {
              //数据为空
              ctx.commit("mutDataStatus", {
                status: "0"
              });
              ctx.commit("mutFinished", {
                finished: true
              });
              return;
            }else{
               //数据不为空
               ctx.commit("mutDataStatus", {
                status: "-1"
              });
              ctx.commit("mutFinished", {
                finished: false
              });
            }
            ctx.commit("mutList", {
              data: res.data.result
            });
            if (res.data.total == ctx.state.list.length) {
              //数据加载完
              ctx.commit("mutFinished", {
                finished: true
              });
              return;
            }
            ctx.state.pageNum++;
          }
        })
        .catch(error => {
          ctx.commit("mutLoading", {
            loading: false
          });
          ctx.commit("mutFinished", {
            finished: true
          });
          ctx.commit("mutDataStatus", {
            status: "1"
          });
        });
    },
    async actList(ctx) {
      //获取默认的公司信息列表
      ctx.commit("mutLoading", {
        loading: true
      });
      // "?pageSize="+ctx.state.pageSize+"&pageNum="+ctx.state.pageNum
      await axios
        .get(
          listApi.findCorpList +
            "?pageSize=" +
            ctx.state.pageSize +
            "&pageNum=" +
            ctx.state.pageNum +
            "&status=" +
            ctx.state.status
        )
        .then(response => {
          let res = response.data;
          if (res.code === 200) {
            if (typeof res.data == "string") {
              ctx.state.updateCorpStatus.text = res.data;
              ctx.commit("mutLoading", {
                loading: false
              });
              ctx.commit("mutFinished", {
                finished: true
              });
              ctx.commit("mutDataStatus", {
                status: "0"
              });
              return null;
            }
            if (
              res.data.result == undefined ||
              (res.data.result.length == "0" && ctx.state.list.length == "0")
            ) {
              //数据为空
              ctx.commit("mutDataStatus", {
                status: "0"
              });
              ctx.commit("mutFinished", {
                finished: true
              });
              return;
            }else{
               //数据不为空
               ctx.commit("mutDataStatus", {
                status: "-1"
              });
              ctx.commit("mutFinished", {
                finished: false
              });
            }
            ctx.commit("mutList", {
              data: res.data.result
            });
            if (res.data.total == ctx.state.list.length) {
              //数据加载完
              ctx.commit("mutFinished", {
                finished: true
              });
              return;
            }
            ctx.state.pageNum++;
          }
        })
        .catch(error => {
          ctx.commit("mutLoading", {
            loading: false
          });
          ctx.commit("mutFinished", {
            finished: true
          });
          ctx.commit("mutDataStatus", {
            status: "1"
          });
        });
    },
    actAddCorp(ctx) {
      //添加企业
      // ctx.state.updateCorpStatus.data["dataStatus"] = "1";
      return axios
        .post(listApi.addCorp, ctx.state.updateCorpStatus.data)
        .then(response => {
          ctx.state.updateCorpStatus.code = response.data.code;
          ctx.state.updateCorpStatus.text = response.data.message;
        })
        .catch(error => {
          ctx.state.updateCorpStatus.text = error;
        });
    },
    actUpdateCorpData(ctx) {
      //修改企业corpId
      return axios
        .post(listApi.updateCorpData, ctx.state.updateCorpStatus.data)
        .then(response => {
          ctx.state.updateCorpStatus.text = response.data.message;
        })
        .catch(error => {
          ctx.state.updateCorpStatus.text = error;
        });
    },
    //删除企业
    artdelete(ctx){
      let data = {
        params: {
          corpId:ctx.state.deleteid
        }
      }
      return axios
        .get(listApi.deleteCorp, data)
        .then(response => {
          ctx.state.updateCorpStatus.code = response.data.code;
          ctx.state.updateCorpStatus.text = response.data.message;
        })
        .catch(error => {
          ctx.state.updateCorpStatus.text = error;
        });
    }
  },
  getters: {
    //get获取数据
    getDataStatus(state) {
      return state.dataStatus;
    },
    getLoading(state) {
      return state.loading;
    },

    getFinished(state) {
      return state.finished;
    },
    getFinishedText(state) {
      return state.finishedText;
    },
    getTitle(state) {
      return state.title;
    },
    getIsSearch(state) {
      //是否是查询标记
      return state.isSearch;
    },
    getList(state) {
      //获取列表
      return state.list;
    },
    getUpdateCorpStatus(state) {
      //添加修改企业状态
      return state.updateCorpStatus;
    }
  }
};

export default enterpriseinfo;
